if not CLIENT then return false end -- Clientside only :)

--[[
made by:
 ____ _______                    _
|  __|__   __|_  _ __   ____  __| |
 \ \    | |/ _ \| '_ \ / __ )/ _` |
 _\ \   | | (_) | | | | (___| (_| |
|____|  |_|\___/|_| |_|\__,_)\__,_|

Credits to FPtje for the main idea, base code

]]

Detector = {}
Detector.Version = 1.4

local proptbl1 = {}
local proptbl2 = {}
local candraw = false
local selectmode = false

function Detector.Create(ply, cmd, args)
	local trace = LocalPlayer():GetEyeTraceNoCursor()
	local prop1 = ents.Create("prop_physics")
	prop1:SetPos(trace.HitPos)
	prop1:SetAngles(trace.HitNormal:Angle())
	prop1:SetModel("models/props_lab/tpplug.mdl")
	prop1:Spawn()
	
	selectmode = true
	
	table.insert(proptbl1, prop1)
	proptbl1[#proptbl1].pos = trace.HitPos + trace.HitNormal * 12
	proptbl1[#proptbl1].ent = {}
	           
	hook.Add("HUDPaint", "DrawHelp", function() 
		draw.DrawText("Klick Mouse1 for the second Detector!", "BudgetLabel", ScrW() / 2, ScrH() / 2, Color(255,255,255,255), 1)
	end)
	
	if (args[1] ~= nil) then
		proptbl1[#proptbl1].name = ""..tostring(table.concat(args, " "))..""
	else
		proptbl1[#proptbl1].name = "my detector"
	end
end
concommand.Add("det_create",Detector.Create)

function Detector.Finish(ply, bind, pressed)
	if (selectmode and ply == LocalPlayer() and pressed) then
		if (string.find(bind, "attack")) then
			hook.Remove("HUDPaint", "DrawHelp")
			
			local trace = LocalPlayer():GetEyeTraceNoCursor()
			local prop2 = ents.Create("prop_physics")
			prop2:SetPos(trace.HitPos)
			prop2:SetAngles(trace.HitNormal:Angle())
			prop2:SetModel("models/props_lab/tpplug.mdl")
			prop2:Spawn()
			
			table.insert(proptbl2, prop2)
			proptbl2[#proptbl2].pos = trace.HitPos + trace.HitNormal * 12
			proptbl2[#proptbl2].ent = {}
			
			candraw = true
			selectmode = false
			return true
		end	
	end
end
hook.Add("PlayerBindPress", "Finish", Detector.Finish)

function Detector.DrawLine()
	if (candraw and #proptbl1 ~= 0 and #proptbl2 ~= 0) then
	for i=1,#proptbl2 do
		local firstpos = proptbl1[i].pos
		local secondpos = proptbl2[i].pos
	    if ((LocalPlayer():GetPos() - firstpos):Length() < 500) then
			local pos = LocalPlayer():EyePos()
			local ang = EyeAngles()
			cam.Start3D(pos, ang)
				render.SetMaterial(Material( "trails/laser" ))
				render.DrawBeam(firstpos, secondpos, 8, 2, 0, Color(0,255,0,255))
			cam.End3D()
		end
    end
    end
end
hook.Add("HUDPaint", "Detector Line", Detector.DrawLine)

function Detector.Remove()
	selectmode = false
	if (#proptbl1 == 0) then	
		LocalPlayer():ChatPrint("No Detectors available!")
	end
	if (#proptbl1 ~= 0 and #proptbl2 ~= 0 and proptbl1[#proptbl1]:IsValid() and proptbl2[#proptbl2]:IsValid()) then
		proptbl1[#proptbl1]:Remove()
		table.remove(proptbl1, #proptbl1)
		proptbl2[#proptbl2]:Remove()
		table.remove(proptbl2, #proptbl2)
		LocalPlayer():ChatPrint("Last detector removed.")
	end
end	
concommand.Add("det_remove",Detector.Remove)

function Detector.Think()
	local alarmsound = CreateSound(LocalPlayer(), Sound("ambient/alarms/siren.wav"))
	if (candraw and #proptbl1 ~= 0 and #proptbl2 ~= 0) then
		for k,det in pairs(proptbl2) do
			local trace = {}
			trace.start = proptbl1[k].pos
			trace.endpos = proptbl2[k].pos
			trace.filter = {LocalPlayer()}
			local tr = util.TraceLine(trace)
			local ent = tr.Entity
			if (ent:IsValid()) then
				if (ent:GetClass() ==  "prop_physics") then
					local text = tostring(ent:GetModel() .. " has entered " .. proptbl1[k].name)
					alarmsound:Play()
					timer.Simple(1, function() alarmsound:Stop() end)
					if (ent:GetNetworkedString("Owner") ~= "") then
						text = tostring(ent:GetModel() .. " of " .. ent:GetNetworkedString("Owner") .. " has entered " .. proptbl1[k].name)
					elseif (ASS_PP_GetOwner and ASS_PP_GetOwner(ent):IsValid()) then
						text = tostring(ent:GetModel() .. " of " .. ASS_PP_GetOwner(ent):Name() .. " has entered " .. proptbl1[k].name)
					end
					RunConsoleCommand("say",text)
				elseif (ent:GetClass() == "player") then
					if (ent == LocalPlayer()) then
						surface.PlaySound("buttons/button14.wav")
					else
						alarmsound:Play()
						timer.Simple(1, function() alarmsound:Stop() end)
						RunConsoleCommand("say",tostring(ent:Name() .. " has entered " ..  proptbl1[k].name))
					end
				elseif (ent:GetClass() ~= "player" and ent:GetClass() ~= "worldspawn" and ent:GetClass() ~= "prop_physics" and not ent:IsWeapon() and ent:GetClass() ~= "viewmodel") then
					alarmsound:Play()
					timer.Simple(1, function() alarmsound:Stop() end)
					RunConsoleCommand("say",tostring(ent:GetClass() .. " has entered " .. proptbl1[k].name))
				end
			end
		end
	end
end
hook.Add("Think","Detector Think",Detector.Think)